package com.lifeverse.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 决策反馈实体类
 * 记录决策执行后的反馈和评估信息
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "decision_feedbacks", indexes = {
    @Index(name = "idx_decision_feedback_decision", columnList = "decision_id"),
    @Index(name = "idx_decision_feedback_timestamp", columnList = "feedback_time")
})
public class DecisionFeedback extends BaseEntity {
    
    /**
     * 关联的决策
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "decision_id", nullable = false)
    private Decision decision;
    
    /**
     * 反馈类型
     */
    @Column(name = "feedback_type", nullable = false, length = 50)
    private String feedbackType;
    
    /**
     * 反馈来源
     */
    @Column(name = "feedback_source", length = 100)
    private String feedbackSource;
    
    /**
     * 反馈内容
     */
    @Column(name = "feedback_content", columnDefinition = "TEXT")
    private String feedbackContent;
    
    /**
     * 反馈评分（1-10）
     */
    @Column(name = "rating", precision = 3, scale = 1)
    private BigDecimal rating;
    
    /**
     * 反馈时间
     */
    @Column(name = "feedback_time", nullable = false)
    private LocalDateTime feedbackTime;
    
    /**
     * 是否为正面反馈
     */
    @Column(name = "is_positive")
    private Boolean isPositive;
    
    /**
     * 影响程度（1-10）
     */
    @Column(name = "impact_level", precision = 3, scale = 1)
    private BigDecimal impactLevel;
    
    /**
     * 改进建议
     */
    @Column(name = "improvement_suggestions", columnDefinition = "TEXT")
    private String improvementSuggestions;
    
    /**
     * 反馈标签（JSON格式）
     */
    @Column(name = "tags", length = 500)
    private String tags;
    
    /**
     * 是否已处理
     */
    @Column(name = "is_processed", nullable = false)
    private Boolean isProcessed = false;
    
    /**
     * 处理时间
     */
    @Column(name = "processed_at")
    private LocalDateTime processedAt;
    
    /**
     * 处理结果
     */
    @Column(name = "processing_result", length = 500)
    private String processingResult;
    
    @PrePersist
    protected void onCreate() {
        super.onCreate();
        if (feedbackTime == null) {
            feedbackTime = LocalDateTime.now();
        }
        if (isProcessed == null) {
            isProcessed = false;
        }
    }
    
    /**
     * 标记为已处理
     */
    public void markProcessed(String result) {
        this.isProcessed = true;
        this.processedAt = LocalDateTime.now();
        this.processingResult = result;
    }
    
    /**
     * 判断是否为高影响反馈
     */
    public boolean isHighImpact() {
        return impactLevel != null && impactLevel.compareTo(BigDecimal.valueOf(7)) >= 0;
    }
    
    /**
     * 创建反馈记录
     */
    public static DecisionFeedback create(Decision decision, String type, String source, 
                                        String content, BigDecimal rating, Boolean isPositive) {
        DecisionFeedback feedback = new DecisionFeedback();
        feedback.setDecision(decision);
        feedback.setFeedbackType(type);
        feedback.setFeedbackSource(source);
        feedback.setFeedbackContent(content);
        feedback.setRating(rating);
        feedback.setIsPositive(isPositive);
        feedback.setFeedbackTime(LocalDateTime.now());
        return feedback;
    }
}